home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 4 / The 640 Meg Shareware Studio CD-ROM Volume IV (Data Express)(1994).ISO / clang / 140_01.zip / DDTTOMAC.C < prev    next >
Text File  |  1993-06-26  |  6KB  |  315 lines

  1. /* program to edit DDT disassembly listing
  2.     - 4 digit hex values converted to labels
  3.     - 2 digit hex values converted to 000H format
  4.     - addresses stripped from instructions
  5.     - lines not commencing with an address commented out
  6.     - labels in assembly range included with instructions
  7.     - labels out of range set by EQU statements */
  8.  
  9. #include <bdscio.h>
  10. #define TAB 0x09
  11. #define HEX "%x"
  12. #define OUTRANGE 1
  13. #define INRANGE 0
  14. #define ENTER 1
  15. #define SEEK 0
  16. #define STMAX 1024
  17. #define BUFFER struct _buf
  18.  
  19. BUFFER    ib,ob,*inbuf,*outbuf;    /* file buffers */
  20. char    *infile,*outfile,    /* file names */
  21.     linebuffer[80],        /* space for line string */
  22.     *line, *linept,        /* line and pointer */
  23.     tokenbuffer[80],    /* space for token string */
  24.     *token,            /* last token read */
  25.     delim;            /* token delimiter */
  26. int    labval,opval,        /* label and operand values */
  27.     disk;            /* write to disk flag */
  28. int    st[STMAX],stp;        /* symbol table and index */
  29. char    stf[STMAX];        /* symbol defined flags */
  30.  
  31. main(argc,argv)
  32.     int    argc;
  33.     char    *argv[];
  34. {
  35.     printf("\nDDTTOMAC version 1.0");
  36.     printf("\n(C) 1981 FBN Software");
  37.     if (argc > 3)
  38.         error("too many parameters");
  39.     if (argc < 2)
  40.         error("no source file specified");
  41.     infile = argv[1];
  42.     if (disk = (argc == 3)) outfile = argv[2];
  43.     inbuf = & ib;
  44.     outbuf = & ob;
  45.     line = &(linebuffer[0]);
  46.     token = &(tokenbuffer[0]);
  47.     stp = 1;
  48.     edit();
  49.     printf("function complete");
  50. }
  51.  
  52.  
  53. edit()
  54. {    /* process passes */
  55.     if (fopen(infile,inbuf) == -1)
  56.         error("source file not found");
  57.     pass1();
  58.     fclose(inbuf);
  59.     if (fopen(infile,inbuf) == -1)
  60.         error("cannot reopen source");
  61.     if (disk)
  62.         if (fcreat(outfile,outbuf) == -1)
  63.             error("no directory space");
  64.     pass2();
  65.     if (disk){
  66.         putc(CPMEOF,outbuf);
  67.         fflush(outbuf);
  68.         fclose(outbuf);
  69.         }
  70. }
  71.  
  72.  
  73. pass1()
  74. {    /* scan through file building symbol table */
  75.     printf("\npass 1\n");
  76.     while (fgets(line,inbuf))
  77.         if (argscan())
  78.             stsearch(opval,ENTER);
  79. }
  80.  
  81.  
  82. pass2()
  83. {    /* rescan using symbol table to perform editing */
  84.     int    i;
  85.     printf("\npass 2\n");
  86.     for (i = 0; i < stp; stf[i++] = OUTRANGE);
  87.     while (fgets(line,inbuf)){
  88.         linept = line;
  89.         gettoken();
  90.         if (isword(&labval)){
  91.             label();
  92.             opcode();
  93.             operand();
  94.             }
  95.         else{
  96.             if (disk) putc(";",outbuf);
  97.             else putchar(';');
  98.             if (disk){
  99.                 if (fputs(line,outbuf) == -1)
  100.                     error("disk full");
  101.                 }
  102.             else puts(line);
  103.             }
  104.         }
  105.     for (i = 1; i < stp; i++)
  106.         if (stf[i] == OUTRANGE) eqline(i);
  107. }
  108.  
  109.  
  110. argscan()
  111. {    /* scan line for hex operand */
  112.     linept = line;
  113.     do gettoken(); while (delim != '\n');
  114.     return isword(&opval);
  115. }
  116.  
  117.  
  118. label()
  119. {
  120.     int    i;
  121.     if (i = stsearch(labval,SEEK)){
  122.         stf[i] = INRANGE;
  123.         makelab(token,i);
  124.         if (disk) fputs(token,outbuf);
  125.         else puts(token);
  126.         if (disk) putc(':',outbuf);
  127.         else putchar(':');
  128.         }
  129. }
  130.  
  131.  
  132. opcode()
  133. {
  134.     if (disk) putc(TAB,outbuf);
  135.     else putchar(TAB);
  136.     gettoken();
  137.     if (match(token,"??")) strcpy(token,"DB");
  138.     if (disk) fputs(token,outbuf);
  139.     else puts(token);
  140. }
  141.  
  142.  
  143. operand()
  144. {
  145.     int    i;
  146.     if (delim != '\n'){
  147.         if (disk) putc(TAB,outbuf);
  148.         else putchar(TAB);
  149.         gettoken();
  150.         if (delim == ','){
  151.             if (disk) fputs(token,outbuf);
  152.             else puts(token);
  153.             if (disk) putc(',',outbuf);
  154.             else putchar(',');
  155.             gettoken();
  156.             }
  157.         if (isbyte(&opval)){
  158.             if (disk) putc('0',outbuf);
  159.             else putchar('0');
  160.             if (disk) fputs(token,outbuf);
  161.             else puts(token);
  162.             if (disk) putc('H',outbuf);
  163.             else putchar('H');
  164.             }
  165.         else{
  166.             if (isword(&opval)){
  167.                 i = stsearch(opval,SEEK);
  168.                 makelab(token,i);
  169.                 }
  170.             if (disk) fputs(token,outbuf);
  171.             else puts(token);
  172.             }
  173.         }
  174.     if (disk){
  175.         if (putc('\n',outbuf) == -1)
  176.             error("disk full");
  177.         }
  178.     else putchar('\n');
  179. }
  180.  
  181.  
  182. gettoken()
  183. {    /* get token from line at linept.  Step linept to
  184.        start of next token.  Set delim. */
  185.     char    c,*tokpt;
  186.     tokpt = token;
  187.     while (*linept == ' ') linept++;
  188.     while (isidchar(c = *(linept++)))
  189.         *(tokpt++) = c;
  190.     *tokpt = 0;
  191.     delim = c;
  192.     while ((c = *linept) == ' ') linept++;
  193.     if (c == '\n') delim = c;
  194. }
  195.  
  196.  
  197. isidchar(c)
  198.     char    c;
  199. {
  200.     if (isalpha(c)) return 1;
  201.     if (isdigit(c)) return 1;
  202.     if (c == '?') return 1;
  203.     return 0;
  204. }
  205.  
  206.  
  207. isword(val)
  208.     int    *val;
  209. {    /* return true & set val if token is a 4 digit hex */
  210.     if (strlen(token) == 4)
  211.         return sscanf(token,HEX,val);
  212.     else return 0;
  213. }
  214.  
  215.  
  216. isbyte(val)
  217.     int    *val;
  218. {    /* return true & set val if token is a 2 digit hex */
  219.     if (strlen(token) == 2)
  220.         return sscanf(token,HEX,val);
  221.     else return 0;
  222. }
  223.  
  224.  
  225. eqline(i)
  226.     int    i;
  227. {    /* symbol i out of label range, generate equate */
  228.     makelab(line,i);
  229.     makehex(token,st[i]);
  230.     if (disk){
  231.         fputs(line,outbuf);
  232.         putc(':',outbuf);
  233.         putc(TAB,outbuf);
  234.         fputs("EQU",outbuf);
  235.         putc(TAB,outbuf);
  236.         fputs(token,outbuf);
  237.         if (putc('\n',outbuf) == -1)
  238.             error("disk full");
  239.         }
  240.     else    {
  241.         puts(line);
  242.         putchar(':');
  243.         putchar(TAB);
  244.         puts("EQU");
  245.         putchar(TAB);
  246.         puts(token);
  247.         putchar('\n');
  248.         }
  249. }
  250.  
  251.  
  252. match(seq,str)
  253.     char    *seq,*str;
  254. {    /* match char sequence against string */
  255.     char    c;
  256.     while (c = *(str++))
  257.         if (c != *(seq++)) return 0;
  258.     return 1;
  259. }
  260.  
  261.  
  262. stsearch(val,insert)
  263.     int    val,insert;
  264. {    /* search symbol table for val returning 0 if not
  265.        found or index to table otherwise. If insert true
  266.        place in table if not there already */
  267.     int    i;
  268.     st[0] = val;
  269.     for (i = stp - 1; ; i--)
  270.         if (val == st[i]) break;
  271.     if (i > 0) return i;
  272.     if (insert == 0) return i;
  273.     st[stp++] = val;
  274.     if (stp > STMAX) error("table full");
  275.     return i;
  276. }
  277.  
  278.  
  279. makehex(str,val)
  280.     char    *str;
  281.     int    val;
  282. {    /* insert val in str in 0nnnH format */
  283.     char    space[8],*sp;
  284.     sp = &(space[1]);
  285.     space[0] = '0';
  286.     sprintf(sp,"%-4x",val);
  287.     while (*sp != ' ') sp++;
  288.     *(sp++) = 'H';
  289.     *sp = 0;
  290.     strcpy(str,&(space[0]));
  291. }    
  292.  
  293.  
  294. makelab(str,val)
  295.     char    *str;
  296.     int    val;
  297. {    /* insert val in str in Lnnn: format */
  298.     char    space[8],*sp;
  299.     sp = &(space[1]);
  300.     space[0] = 'L';
  301.     sprintf(sp,"%-4u",val);
  302.     while (*sp != ' ') sp++;
  303.     *sp = 0;
  304.     strcpy(str,&(space[0]));
  305. }    
  306.  
  307.  
  308. error(msg)
  309.     char    *msg;
  310. {
  311.     printf("\n *** error: ");
  312.     puts(msg);
  313.     exit();
  314. }
  315.